package org.example.datascreen.service.Impl;

import org.example.datascreen.eneity.*;
import org.example.datascreen.repository.*;
import org.example.datascreen.service.SensorDataservice;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class SensorDataserviceImpl implements SensorDataservice {

    @Autowired
    private SoilConductivityDataRespository CDRepository;
    @Autowired
    private SoilMoistureDataRespository MDRepository;
    @Autowired
    private SoilNitrogenDataRespository NDRepository;
    @Autowired
    private SoilPHDataRespository PHRepository;
    @Autowired
    private SoilTemperatureDataRespository TDRepository;
    @Override
    public void saveConductivityData(JSONObject data) {
        SoilConductivityData conductivityData = new SoilConductivityData();
        conductivityData.setCreatedTime(new Date());
        conductivityData.setValue(data.getDouble("value"));

        Device device = new Device();
        device.setId(1L);
        conductivityData.setDevice(device);

        // 保存数据到数据库
        CDRepository.save(conductivityData);

    }

    @Override
    public void saveMoistureData(JSONObject data) {
        SoilMoistureData moistureData = new SoilMoistureData();
        moistureData.setCreatedTime(new Date());
        moistureData.setValue(data.getDouble("value"));

        Device device = new Device();
        device.setId(2L);
        moistureData.setDevice(device);

        // 保存数据到数据库
        MDRepository.save(moistureData);
    }

    @Override
    public void saveNitrogenData(JSONObject data) {
        SoilNitrogenData nitrogenData = new SoilNitrogenData();
        nitrogenData.setCreatedTime(new Date());
        nitrogenData.setValue(data.getDouble("value"));

        Device device = new Device();
        device.setId(3L);
        nitrogenData.setDevice(device);

        // 保存数据到数据库
        NDRepository.save(nitrogenData);
    }

    @Override
    public void savePHData(JSONObject data) {
        SoilPHData phData = new SoilPHData();
        phData.setCreatedTime(new Date());
        phData.setValue(data.getDouble("value"));

        Device device = new Device();
        device.setId(4L);
        phData.setDevice(device);

        // 保存数据到数据库
        PHRepository.save(phData);
    }

    @Override
    public void saveTemperatureData(JSONObject data) {
        SoilTemperatureData temperatureData = new SoilTemperatureData();
        temperatureData.setCreatedTime(new Date());
        temperatureData.setValue(data.getDouble("value"));

        Device device = new Device();
        device.setId(5L);
        temperatureData.setDevice(device);

        // 保存数据到数据库
        TDRepository.save(temperatureData);
    }
}
